<?php
//@app_header;

$js_dir = null;

$base = pathinfo(__file__);
$base = $base['dirname'];
defined('CJAX_ROOT') or define('CJAX_ROOT', realpath($base."/../../"));
defined('CJAX_HOME') or define('CJAX_HOME', realpath($base."/../"));
require_once $base.'/classes/format.class.php';
require_once $base.'/classes/std.class.php';
require_once $base.'/classes/cjax.class.php';
require_once $base.'/classes/plugin.class.php';


class CJAX extends CJAX_FRAMEWORK {

	private static $CJAX;
		
	/**
	* get an instance of CJAX
	* with singleton pattern 
	* @return CJAX_FRAMEWORK OBJECT
	*/
	public static function getInstance()
	{
		if(self::$CJAX) {
			return self::$CJAX;
		}
		$ajax = new CJAX_FRAMEWORK;
		if(!isset($ajax->format) || !$ajax->format) {
			$ajax->format = new cjaxFormat();
			if(!$ajax->isAjaxRequest()) {
				$ajax->clearCache();
				if(defined('AJAX_CD')) {
					@setcookie ('AJAX_CD', AJAX_CD, null, '/');
				} else {
					@setcookie ('AJAX_CD', null, time()-(3600*1000),'/');
				}
			}
		}
		return self::$CJAX = $ajax;
	}
}

function ajax()
{
	return CJAX::getInstance();
}

if(defined('JSDIR')) {
	$js_dir = JSDIR;
} else if(defined('IN_SAMPLES')) {
	$dir = $_SERVER['PHP_SELF'];
	while (strrpos($dir,'cjax/')  !== false || $dir=='') {
			$dir = substr($dir,0,strlen($dir) - 1);
	}
	CJAX_FRAMEWORK::path('http://'.$_SERVER['HTTP_HOST'].$dir);
} else {

	$dir  = 'http://'.dirname($_SERVER['HTTP_HOST'].$_SERVER["SCRIPT_NAME"]).'/cjax/core/js/cjax.js';

	//$info = CJAX_FRAMEWORK::getFile('cjax/core/version.php');

	/*if(trim(strpos($info,"CJAX") !==false)) {
		CJAX_FRAMEWORK::$path = CJAX_FRAMEWORK::remotePath();
	}*/
	if(@is_dir('cjax/')) {
		$js_dir  = "cjax/core/js/";
	} else if(@is_dir('core/js/')) {
		$js_dir  = "core/js/";
	} else if(@is_dir('../cjax')) {
		$js_dir  = "../cjax/core/js/";
	} else {
		$dir = str_replace(DIRECTORY_SEPARATOR,'/',getcwd());
		$f = str_replace(DIRECTORY_SEPARATOR,'/',__file__);
		$f = str_replace($dir,'',$f);
		$f=(dirname(dirname($f))).'/';
		$js_dir  = ltrim($f .'core/js/','/');
	}
	
	$error = error_get_last();
	
	if($error &&  preg_match('%.*(open_basedir).*File\(([^\)]+)%', $error['message'], $match)) {
		
		die( sprintf('Restriction <b>open_basedir</b> is turned on. File or directory %s will not be accessible while this setting is on due to security directory range.', $match[2]) );
	}
}


$ajax = CJAX::getInstance();
$ajax->js($js_dir);